<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,IE=9,chrome=1"><meta name="generator" content="MATLAB R2018b"><title>Untitled</title><style type="text/css">.rtcContent { padding: 30px; } .S0 { margin: 3px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: bold; text-align: left;  }
.S1 { margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: normal; text-align: left;  }
.CodeBlock { background-color: #F7F7F7; margin: 10px 0 10px 0;}
.S2 { border-left: 0.578703701496124px solid rgb(233, 233, 233); border-right: 0.578703701496124px solid rgb(233, 233, 233); border-top: 0.578703701496124px solid rgb(233, 233, 233); border-bottom: 0px none rgb(0, 0, 0); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 17.234001159668px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px;  }
.S3 { border-left: 0.578703701496124px solid rgb(233, 233, 233); border-right: 0.578703701496124px solid rgb(233, 233, 233); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 17.234001159668px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px;  }
.S4 { border-left: 0.578703701496124px solid rgb(233, 233, 233); border-right: 0.578703701496124px solid rgb(233, 233, 233); border-top: 0px none rgb(0, 0, 0); border-bottom: 0.578703701496124px solid rgb(233, 233, 233); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 17.234001159668px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px;  }
.S5 { color: rgb(64, 64, 64); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% / auto padding-box border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234001159668px;  }
.S6 { border-left: 0.578703701496124px solid rgb(233, 233, 233); border-right: 0.578703701496124px solid rgb(233, 233, 233); border-top: 0.578703701496124px solid rgb(233, 233, 233); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 6px 45px 0px 13px; line-height: 17.234001159668px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px;  }
.S7 { margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: normal; text-align: left;  }</style></head><body><div class = rtcContent><h2  class = 'S0'><span>Recap</span></h2><div  class = 'S1'><span>1/</span><span style="vertical-align:-6px"><img src="" width="19" height="19.5" /></span><span> = </span><span style="vertical-align:-15px"><img src="" width="114" height="36.5" /></span></div><div  class = 'S1'><span> 2/</span><span style="vertical-align:-6px"><img src="" width="12" height="19.5" /></span><span>  = </span><span style="vertical-align:-10px"><img src="" width="138" height="28" /></span><span> </span></div><div  class = 'S1'><span>3/ solve</span></div><div  class = 'S1'><span>Now let's replace symbolic variable with discretized value for using MATLAB</span></div><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S2'><span style="white-space: pre;"><span>clear </span><span class="warning_squiggle_rte" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%Pre - fem </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%mesh</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%Material and Length</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> A = 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> L = 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> E = 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> a = 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> R = 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%note that is it not really physics based analysis all this ones...</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%number of elements</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> e = 3;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%discretization step h</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> h = L/e;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%number of node = number of element + 1</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> n = e+1;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%numbering node</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%node 1 start at 0, node n at (n-1)*h</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">for </span><span>i=1:n</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  </span><span class="warning_squiggle_rte">node</span><span>(i) = (i-1)*h;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%numbering element &amp; connectivity</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%element 1 connectivity : [1 2] ... element e [e e+1] </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(34, 139, 34);">%means [n-1 n]</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">for </span><span>i=1:e</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  </span><span class="warning_squiggle_rte">elem</span><span>(i,:) = [i i+1];</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%Assembly</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%initialize with 0 matrix</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> K = zeros(n);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> f = zeros(n,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%loop on element form 1 to e</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">for </span><span>i=1:e</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>    node1 = elem(i,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  node2 = elem(i,2);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  </span><span style="color: rgb(34, 139, 34);">%compute once K and f</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  Ke = elementStiffness(A, E, h);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  fe = elementLoad(node(node1),node(node2), a, h);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  </span><span style="color: rgb(34, 139, 34);">%assembly</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  K(node1:node2,node1:node2) = K(node1:node2,node1:node2) + Ke;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  f(node1:node2) = f(node1:node2) + fe;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">% %BCs &amp;Loads</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  f(n) = f(n) + 1.0;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  Kred = K(2:n,2:n);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  fred = f(2:n);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">% Solver</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  d = </span><span class="warning_squiggle_rte">inv</span><span>(Kred)*fred;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span style="color: rgb(34, 139, 34);">%  %d=Kred\fred</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  dsol = [0 d'];</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> fsol = K*dsol';</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S4'><span style="white-space: pre;"><span> sum(fsol)</span></span></div><div  class = 'S5'><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; '>ans = -4.4409e-16</div></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> figure;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> p0 = plotDisp(E, A, L, R, a);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> p1 = plot(node, dsol, </span><span style="color: rgb(160, 32, 240);">'ro--'</span><span>, </span><span style="color: rgb(160, 32, 240);">'LineWidth'</span><span>, 3); hold </span><span style="color: rgb(160, 32, 240);">on</span><span>;</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S4'><span style="white-space: pre;"><span> legend([p0 p1],</span><span style="color: rgb(160, 32, 240);">'Exact'</span><span>,</span><span style="color: rgb(160, 32, 240);">'FEM'</span><span>);</span></span></div><div  class = 'S5'><div class="inlineElement eoOutputWrapper embeddedOutputsFigure" uid="B7A7A7EC" data-scroll-top="null" data-scroll-left="null" data-testid="output_1" style="width: 312px; opacity: 1;"><div class="figureElement" style="cursor: default;"><div class="figureContainingNode" style="width: 560px; max-width: 100%; display: inline-block;"><div class="GraphicsView" data-dojo-attach-point="graphicsViewNode,backgroundColorNode" id="uniqName_197_0" widgetid="uniqName_197_0" style="width: 100%; height: auto;"><img class="ImageView figureImage" data-dojo-attach-point="imageViewNode" draggable="false" ondragstart="return false;" id="uniqName_197_2" widgetid="uniqName_197_2" src="" style="width: 100%; height: auto;"></div></div></div></div></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> </span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">for </span><span>i=1:e</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  node1 = elem(i,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  node2 = elem(i,2);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  u1 = dsol(node1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  u2 = dsol(node2);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  [</span><span class="warning_squiggle_rte">eps</span><span>(i), </span><span class="warning_squiggle_rte">sig</span><span>(i)] = elementStrainStress(u1, u2, E, h);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> figure;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> p0 = plotStress(E, A, L, R, a);</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">for </span><span>i=1:e</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  node1 = node(elem(i,1));</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  node2 = node(elem(i,2));</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span>  p1 = plot([node1 node2], [sig(i) sig(i)], </span><span style="color: rgb(160, 32, 240);">'r-'</span><span>,</span><span style="color: rgb(160, 32, 240);">'LineWidth'</span><span>,3); hold </span><span style="color: rgb(160, 32, 240);">on</span><span>;</span></span></div></div><div class="inlineWrapper"><div  class = 'S3'><span style="white-space: pre;"><span> </span><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S4'><span style="white-space: pre;"><span> legend([p0 p1],</span><span style="color: rgb(160, 32, 240);">'Exact'</span><span>,</span><span style="color: rgb(160, 32, 240);">'FEM'</span><span>);</span></span></div><div  class = 'S5'><div class="inlineElement eoOutputWrapper embeddedOutputsFigure" uid="5A009A94" data-scroll-top="null" data-scroll-left="null" data-testid="output_2" style="width: 312px;"><div class="figureElement" style="cursor: default;"><div class="figureContainingNode" style="width: 560px; max-width: 100%; display: inline-block;"><div class="GraphicsView" data-dojo-attach-point="graphicsViewNode,backgroundColorNode" id="uniqName_197_3" widgetid="uniqName_197_3" style="width: 100%; height: auto;"><img class="ImageView figureImage" data-dojo-attach-point="imageViewNode" draggable="false" ondragstart="return false;" id="uniqName_197_5" widgetid="uniqName_197_5" src="" style="width: 100%; height: auto;"></div></div></div></div></div></div></div><div  class = 'S7'><span>That's all</span></div></div><br>
<!-- 
##### SOURCE BEGIN #####
%% Recap
% 1/$K_{ij}$ = $\int_{0}^{L}\frac{dN_j}{dx} AE \frac{dN_i}{dx}~dx$
% 
%  2/$f_j$  = $\int_0^LN_j{q}~dx + \left. N_j{R}\right|_{x=L}~$ 
% 
% 3/ solve
% 
% Now let's replace symbolic variable with discretized value for using MATLAB

clear all; close all;
%
%Pre - fem 
%mesh
%Material and Length
%
 A = 1.0;
 L = 1.0;
 E = 1.0;
 a = 1.0;
 R = 1.0;
 %note that is it not really physics based analysis all this ones...

 %number of elements
 e = 3;
 %discretization step h
 h = L/e;
 %number of node = number of element + 1
 n = e+1;

 %numbering node
 %node 1 start at 0, node n at (n-1)*h
 for i=1:n
  node(i) = (i-1)*h;
 end

 %numbering element & connectivity
 %element 1 connectivity : [1 2] ... element e [e e+1] 
 %means [n-1 n]
 for i=1:e
  elem(i,:) = [i i+1];
 end

%
%Assembly
%
%initialize with 0 matrix
 K = zeros(n);
 f = zeros(n,1);
 
%loop on element form 1 to e
 for i=1:e
    node1 = elem(i,1);
  node2 = elem(i,2);
  %compute once K and f
  Ke = elementStiffness(A, E, h);
  fe = elementLoad(node(node1),node(node2), a, h);
  %assembly
  K(node1:node2,node1:node2) = K(node1:node2,node1:node2) + Ke;
  f(node1:node2) = f(node1:node2) + fe;
 end


% %BCs &Loads

  f(n) = f(n) + 1.0;

  Kred = K(2:n,2:n);
  fred = f(2:n);


% Solver

 
  d = inv(Kred)*fred;
%  %d=Kred\fred
 
  dsol = [0 d'];
 
 fsol = K*dsol';
 sum(fsol)
 figure;
 p0 = plotDisp(E, A, L, R, a);
 p1 = plot(node, dsol, 'roREPLACE_WITH_DASH_DASH', 'LineWidth', 3); hold on;
 legend([p0 p1],'Exact','FEM');
 
 for i=1:e
  node1 = elem(i,1);
  node2 = elem(i,2);
  u1 = dsol(node1);
  u2 = dsol(node2);
  [eps(i), sig(i)] = elementStrainStress(u1, u2, E, h);
 end

 figure;
 p0 = plotStress(E, A, L, R, a);
 for i=1:e
  node1 = node(elem(i,1));
  node2 = node(elem(i,2));
  p1 = plot([node1 node2], [sig(i) sig(i)], 'r-','LineWidth',3); hold on;
 end
 legend([p0 p1],'Exact','FEM');
%% 
% That's all
##### SOURCE END #####
--></body></html>